<template>
  <div class="batch-query-container">
    <!-- 搜索条件区域 -->
    <div class="search-section">
      <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
        <!-- 第一行搜索条件 -->
        <div class="search-row">
          <el-form-item label="货品" prop="productName">
            <el-input 
              v-model="searchForm.productName" 
              placeholder="货品名称/编号"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          
          <el-form-item label="货品类型" prop="productTypeId">
            <el-select 
              v-model="searchForm.productTypeId" 
              placeholder="请选择"
              clearable
              style="width: 150px"
            >
              <el-option label="电子产品" value="电子产品" />
              <el-option label="家电" value="家电" />
              <el-option label="服装" value="服装" />
              <el-option label="食品" value="食品" />
              <el-option label="家具" value="家具" />
              <el-option label="化妆品" value="化妆品" />
              <el-option label="鞋类" value="鞋类" />
              <el-option label="配饰" value="配饰" />
              <el-option label="玩具" value="玩具" />
              <el-option label="书籍" value="书籍" />
              <el-option label="运动器材" value="运动器材" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="单位" prop="unitName">
            <el-select 
              v-model="searchForm.unitName" 
              placeholder="请选择"
              clearable
              style="width: 120px"
            >
              <el-option label="个" value="个" />
              <el-option label="件" value="件" />
              <el-option label="台" value="台" />
              <el-option label="套" value="套" />
              <el-option label="双" value="双" />
              <el-option label="本" value="本" />
              <el-option label="瓶" value="瓶" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="货品批次" prop="batch">
            <el-input 
              v-model="searchForm.batch" 
              placeholder="批次"
              clearable
              style="width: 150px"
            />
          </el-form-item>
        </div>
        
        <!-- 第二行搜索条件 -->
        <div class="search-row">
          <el-form-item label="仓库名称" prop="inventory">
            <el-select 
              v-model="searchForm.inventory" 
              placeholder="请选择"
              clearable
              style="width: 150px"
            >
              <el-option label="仓库一" value="仓库一" />
              <el-option label="仓库二" value="仓库二" />
              <el-option label="仓库三" value="仓库三" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="生产日期" prop="productiondate">
            <el-date-picker
              v-model="searchForm.productiondate"
              type="date"
              placeholder="请选择"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 150px"
            />
          </el-form-item>
          
          <el-form-item label="到期日期" prop="expirationdate">
            <el-date-picker
              v-model="searchForm.expirationdate"
              type="date"
              placeholder="请选择"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 150px"
            />
          </el-form-item>
        </div>
        
        <!-- 操作按钮 -->
        <div class="search-actions">
          <div class="left-actions">
            <el-button type="default" @click="handleExport">导出</el-button>
            <el-button @click="handleRefresh">刷新</el-button>
          </div>
          <div class="right-actions">
            <el-button type="default" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <!-- 数据表格区域 -->
    <div class="table-section">
      <el-table 
        :data="tableData" 
        v-loading="loading"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="index" label="序号" width="80" />
        <el-table-column prop="productNo" label="货品编号" width="120" />
        <el-table-column prop="productName" label="货品名称" width="150" />
        <el-table-column prop="productTypeId" label="货品类型" width="100" />
        <el-table-column prop="productModel" label="规格型号" width="120" />
        <el-table-column prop="unitName" label="单位" width="80" />
        <el-table-column prop="inventory" label="仓库名称" width="120" />
        <el-table-column prop="current_Inventory" label="当前库存" width="100" />
        <el-table-column prop="batch" label="批次" width="120" />
        <el-table-column prop="productiondate" label="生产日期" width="120">
          <template #default="scope">
            {{ formatDate(scope.row.productiondate) }}
          </template>
        </el-table-column>
        <el-table-column prop="expirationdate" label="到期日期" width="120">
          <template #default="scope">
            {{ formatDate(scope.row.expirationdate) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="scope">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleView(scope.row)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-section">
      <el-pagination
        v-model:current-page="pagination.pageIndex"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { TZRead } from '@/http/interceptors'
import moment from 'moment'
import { useRouter } from 'vue-router'
const router=useRouter();

// 查询表单数据
const searchForm = reactive({
  productName: '',
  productTypeId: '',
  unitName: '',
  batch: '',
  inventory: '',
  productiondate: '',
  expirationdate: ''
})

// 分页数据
const pagination = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 表单引用
const searchFormRef = ref()

// 日期格式化函数
const formatDate = (date) => {
  if (!date) return ''
  return moment(date).format('YYYY-MM-DD')
}

// 查询数据
const loadData = async () => {
  loading.value = true
  try {
    // 构建查询参数
    const params = {
      Pageindex: pagination.pageIndex,
      Pagesize: pagination.pageSize,
      ProductName: searchForm.productName || undefined,
      ProductTypeId: searchForm.productTypeId || undefined,
      UnitName: searchForm.unitName || undefined,
      Batch: searchForm.batch || undefined,
      Inventory: searchForm.inventory || undefined,
      Productiondate: searchForm.productiondate || undefined,
      Expirationdate: searchForm.expirationdate || undefined
    }
    
    // 移除空值参数
    Object.keys(params).forEach(key => {
      if (params[key] === '' || params[key] === null || params[key] === undefined) {
        delete params[key]
      }
    })

    // 调用批次查询API接口
    const res = await TZRead.get("/api/Product/batch-list", { params })
    
    if (res.data.code === 200) {
      // 处理数据：添加序号
      const processedData = res.data.pageData.map((item, index) => {
        return {
          ...item,
          index: (pagination.pageIndex - 1) * pagination.pageSize + index + 1
        }
      })
      
      tableData.value = processedData
      pagination.total = res.data.totalCount
      
      ElMessage.success('查询成功')
    } else {
      ElMessage.error(res.data.message || '查询失败')
    }
  } catch (error) {
    ElMessage.error('查询失败：' + error.message)
  } finally {
    loading.value = false
  }
}

// 查询按钮
const handleSearch = () => {
  pagination.pageIndex = 1
  loadData()
}

// 重置按钮
const handleReset = () => {
  searchFormRef.value?.resetFields()
  pagination.pageIndex = 1
  loadData()
}

// 刷新按钮
const handleRefresh = () => {
  loadData()
}

// 导出按钮
const handleExport = () => {
  ElMessage.info('导出功能待实现')
}

// 查看按钮
const handleView = (row) => {
   router.push({
    path: '/batchflow',
    query: { Batch_Id: row.batch}
  })
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.pageSize = size
  pagination.pageIndex = 1
  loadData()
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.pageIndex = page
  loadData()
}

// 页面加载时执行
onMounted(async () => {
  await loadData()
})
</script>

<style scoped>
.batch-query-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.search-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.search-row {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
}

.search-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.left-actions {
  display: flex;
  gap: 10px;
}

.right-actions {
  display: flex;
  gap: 10px;
}

.table-section {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  overflow: hidden;
}

.pagination-section {
  display: flex;
  justify-content: center;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style> 